package cn.bdqn.fin.service.impl;

import cn.bdqn.fin.mapper.ApprovalMapper;
import cn.bdqn.fin.mapper.VApprovalMapper;
import cn.bdqn.fin.model.*;
import cn.bdqn.fin.service.ApprovalService;
import cn.bdqn.fin.vomapper.ApplyVoMapper;
import cn.bdqn.fin.vomapper.PaymentTypeVoMapper;
import cn.bdqn.model.EUList;
import cn.bdqn.model.PageBean;
import cn.bdqn.model.R;
import cn.bdqn.service.BaseService;
import cn.bdqn.veh.model.Leader;
import cn.bdqn.veh.vomapper.VehicleVoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author gurilla
 * @create 2019-08-23 16:20
 */
@Service
public class ApprovalServiceImpl extends BaseService implements ApprovalService {
    @Resource
    private VApprovalMapper vApprovalMapper;

    @Resource
    private PaymentTypeVoMapper paymentTypeVoMapper;

    @Resource
    private VehicleVoMapper vehicleVoMapper;

    @Resource
    private ApprovalMapper approvalMapper;

    @Resource
    private ApplyVoMapper applyVoMapper;

    @Override
    public EUList<VApproval> list(PageBean pageBean, String lname, String phone, String account, String drawee ) {
        VApprovalExample example = new VApprovalExample();
        VApprovalExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(lname) || !StringUtils.isEmpty(account) || !StringUtils.isEmpty(phone) || !StringUtils.isEmpty(drawee)){
            criteria.andLnameLike("%"+lname+"%");
            criteria.andAccountLike("%"+account+"%");
            criteria.andPhoneLike("%" + phone + "%");
            criteria.andDraweeLike("%" + drawee + "%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        List<VApproval> vApprovals = vApprovalMapper.selectByExample(example);
        PageInfo<VApproval> info=new PageInfo<>(vApprovals);
        return new EUList<>(info.getTotal(),info.getList());
    }

    @Override
    public R save(Approval approval) {
//        VApproval vApproval = new VApproval();
        if(approval.getId()==null){
            approval.setPaymentStatus(false);
            approval.setIsbill(false);
            approval.setIsrejected(false);
            approval.setOpid(getUserid());
            approval.setCreateTime(new Date());
            approval.setLastTime(new Date());
            approvalMapper.insertSelective(approval);
        }else{
 //           approval.setId(vApproval.getId());
            approval.setOpid(getUserid());
            approval.setLastTime(new Date());
            approvalMapper.updateByPrimaryKeySelective(approval);
        }
        return R.ok();
    }

    @Override
    public R getLeader() {
        List<Leader> leaders = vehicleVoMapper.getAllLeader();
        return R.ok(null, leaders);
    }

    @Override
    public R getPaymentType() {
        List<PaymentType> paymentTypes = paymentTypeVoMapper.getPaymentType();
        return R.ok(null, paymentTypes);
    }

    @Override
    public R getApply() {
        List<Apply> applies = applyVoMapper.getApply();
        return R.ok(null, applies);
    }
}
